require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsFriendLoveHandler ={};
local this = RedDotsFriendLoveHandler;

function RedDotsFriendLoveHandler.init()
    CloseEvent(Config.EventType.Fresh_MyFriends,this.update);
    ListenEvent(Config.EventType.Fresh_MyFriends,this.update);

    return this;
end

--参数1： playerId，nil检查所有
function RedDotsFriendLoveHandler.getFlag(...)
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Friend,false) then
        local playerId = ...;
        local flag = Config.RedDotsFlagState.None;
        local friendList = logicMgr.FriendManager.GetMyFriendList()
        if friendList ~=nil then
            if playerId~=nil then
                for key, value in pairs(friendList) do
                    if value.playerId == playerId then
                        flag = this.getFlagByFriendData(value);
                        break;
                    end
                end
            else
                local point = logicMgr.FriendManager.GetTodayFriendShip();
                if point >= 30 then
                    return  Config.RedDotsFlagState.None;
                end
                for key, value in pairs(friendList) do
                    flag = this.getFlagByFriendData(value);
                    if flag == Config.RedDotsFlagState.Needful then
                        break;
                    end
                end
            end
        end
    return flag;
    end
end

function RedDotsFriendLoveHandler.getFlagByFriendData(data)
    if data.getLoveState == EnumConst.LoveStateEnum.NORMAL then
        return Config.RedDotsFlagState.Needful;
    else
        return Config.RedDotsFlagState.None;
    end
end

function RedDotsFriendLoveHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.FriendLove);
end
